//
//  UpdatePersonalPassViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/7/31.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "UpdatePersonalPassViewController.h"
#import "SwitchButton.h"
#import "ResMode.h"
@interface UpdatePersonalPassViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet SwitchButton *switchbtn;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation UpdatePersonalPassViewController
- (IBAction)switchckick:(SwitchButton *)sender {
    sender.isON=!sender.isON;
    if (sender.isON) {
        [sender setImage:[UIImage imageNamed:@"xsmm_03"] forState:UIControlStateNormal];
    }else{
         [sender setImage:[UIImage imageNamed:@"ycmm_03"] forState:UIControlStateNormal];
    }
    self.password.secureTextEntry=!sender.isON;
    
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.password endEditing:YES];
}
+(instancetype)viewcontroller{
    UpdatePersonalPassViewController *updatevc=[XHMianStory instantiateViewControllerWithIdentifier:@"updatepersonal"];
    return updatevc;
}
- (IBAction)nextclick:(id)sender {
    if ([self checkform]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在修改中...";
        hud.mode = MBProgressHUDModeIndeterminate;
       
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=@"person";
        params[@"p_action"]=@"pupdate";
        
        NSString *passstr=[NSString stringWithFormat:@"%@%@",self.usermode.user_email,self.password.text];
        params[@"where"]=self.password.text;
        NSLog(@"%@",passstr);
        params[@"user_id"]=self.usermode.user_id;
        [PostUitls Post:makepassurl parameters:params success:^(id response) {
            NSDictionary *dict=response;
            ResMode*resmode=[ResMode modeWithDict:dict];
            if ([resmode.state integerValue]==400) {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"密码找回成功" message:@"请使用新密码登陆" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
                [alert show];
                [hud hide:YES];
            }else{
                hud.mode=MBProgressHUDModeText;
                hud.labelText=resmode.msg;
                [hud hide:YES afterDelay:1];
            }
        } failure:^(NSError *error) {
            hud.mode=MBProgressHUDModeText;
            hud.labelText=@"网络错误";
            [hud hide:YES afterDelay:1];
        }];
    }
}



-(BOOL)checkform{
    if (self.password.text.length<6||![NSString validatePassword:self.password.text]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"密码格式不正确";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.switchbtn.isON=NO;
    self.navigationItem.title=@"重置密码";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
